package com.yoomiito.app.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import com.yoomiito.app.R
import com.yoomiito.app.utils.DensityUtil

class CouponView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0, defStyleRes: Int = 0)
    :LinearLayout(context,attrs, defStyleAttr, defStyleRes){
    private val tv1 = TextView(context)
    private val tv2: TextView = TextView(context)
    private var borderColor = 0
    private var couponText = ""
    private var couponTextBgColor = 0
    private var couponTextSize = 0
    init {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.CouponView)
        borderColor = ta.getColor(R.styleable.CouponView_border_color,getContext().resources.getColor(R.color.color_ff5100))
        couponText = ta.getText(R.styleable.CouponView_coupon_text).toString()
        couponTextBgColor = ta.getColor(R.styleable.CouponView_coupon_text_bg_color,getContext().resources.getColor(R.color.color_ff5100))
        couponTextSize = ta.getDimensionPixelSize(R.styleable.CouponView_coupon_text_size,DensityUtil.dp2px(10f))
        ta.recycle()

        initView()
    }

    private fun initView() {
        val drawableBg = GradientDrawable()
        drawableBg.cornerRadius = DensityUtil.dp2px(2f).toFloat()
        drawableBg.setColor(Color.TRANSPARENT)
        drawableBg.setStroke(DensityUtil.dp2px(0.5f),borderColor)
        background = drawableBg


        tv1.text = couponText
        tv1.setPadding(DensityUtil.dp2px(4f),0,DensityUtil.dp2px(4f),0)
        tv1.setTextColor(borderColor)
        tv1.setTextSize(TypedValue.COMPLEX_UNIT_DIP,10f)
        tv1.setBackgroundColor(couponTextBgColor)
        tv1.gravity = Gravity.CENTER
        val lp1 = LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT)
        addView(tv1,lp1)

        tv2.setTextColor(borderColor)
        tv2.setTextSize(TypedValue.COMPLEX_UNIT_DIP,10f)
        tv2.gravity = Gravity.CENTER
        tv2.setPadding(DensityUtil.dp2px(4f),DensityUtil.dp2px(2f),DensityUtil.dp2px(4f),DensityUtil.dp2px(2f))
        val lp2 = LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT)
        addView(tv2,lp2)

    }

    fun setText(text:String){
        tv2.text = text
    }

    fun setTextSize(size:Float){
        tv1.textSize = size
        tv2.textSize = size
    }

    /**
     * 初始化完成后使用
     */
    fun setColor(borderColor:Int,couponTextBgColor:Int){
        val drawableBg = background as GradientDrawable
        drawableBg.setStroke(DensityUtil.dp2px(0.5f),borderColor)
        tv1.setBackgroundColor(couponTextBgColor)
        tv1.setTextColor(borderColor)
        tv2.setTextColor(borderColor)
    }

    fun setHeaderText(text: String){
        tv1.text = text
    }

    fun setHeaderTextVisible(show:Boolean){
        tv1.visibility = if (show) View.VISIBLE else View.GONE
    }

}